<div class="panel panel-default">
	<div class="panel-heading newsfeed">
		<p>Inbox</p>
	</div>
	<div class="panel-body mail">
		<div class="row">
			<div class="col-sm-3 col-md-3">
				<!--<a href="#" class="btn btn-danger btn-sm btn-block" role="button">COMPOSE</a>-->
			</div>
			<div class="col-sm-9 col-md-9">
				<!-- /input-group -->
				<!-- Split button -->
				<div class="btn-group">
					<span class="btn btn-default">
						<input type="checkbox" id="selectall" />
					</span>  
					
				</div>
				<button type="button" class="btn btn-default" data-toggle="tooltip" title="Refresh">
					<span class="glyphicon glyphicon-refresh"></span> 
				</button>
				<!-- Single button -->
				<div class="btn-group">
					<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
						More <span class="caret"></span>
					</button>
					<ul class="dropdown-menu movelabel" role="menu">
						<li>&nbsp;&nbsp;Move to:</li>
						<li><a href="#" onclick="movetolabel('0')">Inbox</a>
						<?php foreach($labels as $label){?>
						<li><a href="#" onclick="movetolabel('<?=$label['labelid']?>')"><?=$label['label_name']?>
							</a>
							<div class="" style="position:absolute;right:5px;top:4px;">
							<span class="glyphicon glyphicon-pencil editlabel" labelname= "<?=$label['label_name']?>" labelid="<?=$label['labelid']?>"></span>
							<span class="glyphicon glyphicon-remove deletelabel" labelname= "<?=$label['label_name']?>" labelid="<?=$label['labelid']?>"></span>
							</div>
						</li>
						<?}?>
						<li class="divider"></li>
						<li><a href="#" id="createlabel">Create New Label</a></li>
					</ul>
				</div>
				<button type="button" class="btn btn-danger delete">
					Delete
				</button>
				<!--<div class="pull-right">
					<span class="text-muted"><b>1</b>–<b>50</b> of <b>277</b></span>
					<div class="btn-group btn-group-sm">
						<button type="button" class="btn btn-default">
							<span class="glyphicon glyphicon-chevron-left"></span>
						</button>
						<button type="button" class="btn btn-default">
							<span class="glyphicon glyphicon-chevron-right"></span>
						</button>
					</div>
				</div>-->
				<div class="modal fade" id="label" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
					<div class="modal-dialog">
						<div class="modal-content">
							<div class="modal-header">
								<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
								<h4 class="modal-title" id="myModalLabel">New Label</h4>
							</div>
							<form role="form" id="labelform" action="<?php echo base_url('users/createlabel');?>" method="post">
								<div class="modal-body">
									<div class="form-group">
										<label for="exampleInputEmail1">Please enter a new label name:</label>
										<input type="text" class="form-control" id="label_name" name="label_name" placeholder="Enter Label Name">
									</div>
								</div>
								<div class="modal-footer">
									<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
									<button type="submit" class="btn btn-primary">Create</button>
								</div>
							</form>
						</div>
					</div>
				</div>
			</div>
		</div>
		<hr />
		<div class="row">
			<div class="col-sm-3 col-md-3">
				<?php echo $this->template->widget("cms_appmenu_widget", array("menu_name"=>"mail_menu"));?>
			</div>
			<div class="col-sm-9 col-md-9">
				<!-- Nav tabs -->
				<ul class="nav nav-tabs">
					<li class="active"><a href="#home" data-toggle="tab"><span class="glyphicon glyphicon-inbox">
					</span>Primary</a>
					</li>
					<?php foreach($labels as $label){?>
					<li><a href="#<?=$label['label_name']?>" data-toggle="tab"><span class="glyphicon glyphicon-user"></span>
						<?=$label['label_name']?></a>
					</li>
					<?}?>
					
					
				</ul>
				<!-- Tab panes -->
				<div class="tab-content">
					<div class="tab-pane fade in active" id="home">
						<?php echo form_open(null, 'id="inboxform"'); ?>
						
						<table class="table table-striped" style="border:1px solid #dddddd">
							<?php foreach($inboxmail as $inbox){
							if($inbox['openstatus']==0)
							echo "<style>.current{font-weight:bold};</style>";
							?>
							<tr>
								<td class="center"><input type="checkbox" value="<?php echo $inbox['mailid'] ?>" name="selected[]" /></td>
								
								
								<td><a href="<?php echo base_url('users/message_details/'.$inbox['mailid'])?>" >
									<span class="name current" style="min-width: 120px;display: inline-block;"><?=$inbox['firstname']?></span></a></td>
								<td><a href="<?php echo base_url('users/message_details/'.$inbox['mailid'])?>" >
									<span class="msg current"><?php echo substr(strip_tags(stripslashes($inbox['message'])), 0, 50) . '..'; ?></span></a></td>
								
								<td><span class="badge"><?=date('m/d/Y h:i A', strtotime($inbox['date_added']));?></span>  </td>
								</a>
							</tr>
							<?}?>
						</table>
						
						
						
						<?php echo form_close(); ?>
					</div>
					<?php foreach($labels as $label){?>
					<div class="tab-pane fade in" id="<?=$label['label_name']?>">
						<?php echo form_open(null, 'id="'.str_replace(' ', '', $label['label_name']).'form"'); ?>
						
						<table class="table table-striped" style="border:1px solid #dddddd">
							<?php foreach($labelmail as $lmail){
								if($lmail['label']==$label['label_name']){
							?>
							<tr>
								<td class="center"><input type="checkbox" value="<?php echo $lmail['mailid'] ?>" name="selected[]" /></td>
								
								
								<td><a href="<?php echo base_url('users/message_details/'.$lmail['mailid'])?>" >
									<span class="name current" style="min-width: 120px;display: inline-block;"><?=$lmail['firstname']?></span></a></td>
								<td><a href="<?php echo base_url('users/message_details/'.$lmail['mailid'])?>" >
									<span class="msg current"><?php echo substr(strip_tags(stripslashes($lmail['message'])), 0, 50) . '..'; ?></span></a></td>
								
								<td><span class="badge"><?=date('m/d/Y h:i A', strtotime($lmail['date_added']));?></span>  </td>
								</a>
							</tr>
							<?}}?>
						</table>
						<?php echo form_close(); ?>
					</div>
					<?}?>
					
				</div>
			</div>
		</div>
	</div>
</div>
<script>
$(document).ready(function() {
    $('#selectall').click(function(event) {  //on click
        if(this.checked) { // check select status
            $(".tab-content .active").find($('input[name*=\'selected\']')).each(function() { //loop through each checkbox
                this.checked = true;  //select all checkboxes with class "checkbox1"              
            });
        }else{
            $(".tab-content .active").find($('input[name*=\'selected\']')).each(function() { //loop through each checkbox
                this.checked = false; //deselect all checkboxes with class "checkbox1"                      
            });        
        }
    });
	$("#createlabel").click(function(event) {
		$('#label').modal('show');
		$('#label #myModalLabel').html("Create Label");
		$("#labelform #label_name").val("");
		$("#labelform").attr('action', '<?php echo base_url('users/createlabel'); ?>');
		
	});
   $('.editlabel').click(function(event) {
		var labelid=$(this).attr("labelid");
		var labelname=$(this).attr("labelname");
		$('#label').modal('show');
		$('#label #myModalLabel').html("Update Label");
		$("#labelform").attr('action', '<?php echo base_url('users/editlabel').'/'; ?>'+labelid);
		$("#labelform #label_name").val(labelname);
	});
	$(".deletelabel").click(function(event) {
		$.ajax({
				url: '<?php echo base_url()?>ajax/deletelabel',
				data:{labelid:$(this).attr("labelid")},
				type: "POST",
				beforeSend: function(){
					
				},
				success: function(data) {
					if(data=="deleted")
					{
						window.location="<?php echo base_url('users/inbox');?>";
					}
				},
				
			});
	})
	
});
function movetolabel(labelid)
{
	var checklength=$(".tab-content .active").find($('input[name*=\'selected\']:checked')).length;
	if(checklength==0)
	{
		alert("please select checkbox");
	}
	else
	{
		//alert($(".tab-content .active").find($('input[name*=\'selected\']:checked')).val());
		var mailid=$(".tab-content .active").find($('input[name*=\'selected\']:checked')).map(function() {return this.value;}).get().join(',');
		
		$.ajax({
				url: '<?php echo base_url()?>ajax/movetolabel',
				data:{labelid:labelid,mailid:mailid},
				type: "POST",
				beforeSend: function(){
					
				},
				success: function(data) {
					if(data=="moved")
					{
						window.location="<?php echo base_url('users/inbox');?>";
					}
				},
				
			});
	}
		/**/
}
 // Delete
        $('.delete').click( function() {
            if (confirm('Delete cannot be undone! if users delete then corresponding business delete. Are you sure you want to do this?'))
            {
                $(".tab-content .active").find('form').attr('action', '<?php echo base_url('users/deletemail'); ?>').submit()
            }
            else
            {
                return false;
            }
        });

</script>
  
